<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Leader extends MY_Controller {
	function __construct(){
		parent::__construct();
	}
	
	public function index(){
		$lastid=$this->input->get('lastid');

		$where=array();
		$where['isleader > ']=0;
		//$where['mw.id=']='(SELECT MAX(id) FROM `sk_works` b WHERE b.mid=`mw`.`mid` )';
		$and='';
		if(is_numeric($lastid)){
			$and =" and  b.mid<".$lastid;
		}
		//提取会员列表。，排序按照最新更新的作品排序
		$mlist= $this->Data_model->db->query('SELECT `m`.* ,`mw`.`id`,`mw`.`goodnum`,`mw`.`watchnum`,`mw`.`viewnum`,`mw`.`commnum`,`mw`.`shares` FROM (`sk_member` m) LEFT JOIN `sk_works` mw ON `mw`.`mid`=`m`.`mid` WHERE `isleader`>0 AND `mw`.`id`=(SELECT MAX(id) FROM  `sk_works` b where b.mid=`mw`.`mid` '.$and.' ) order by  `mw`.`id` desc limit 2')->result_array();
		//echo $this->Data_model->getSQL();
		//$mlist=array_values($mlist);
		$ud=$this->getUnionData($mlist);
		foreach ($mlist as $key => $value) {
			if(empty($value['mnick']))$mlist[$key]['mnick']=$value['mname'];
			
			$sworks=$this->Data_model->getData(array(
				'mid'=>$value['mid']
				),'iscomm DESC,id DESC',0,5,'works');
			foreach ($sworks as $k => $work) {
				if($this->member->mid==$work['mid']){
					$sworks[$k]['href']=site_url('member/work/'.$work['id']);
				}else{
					$sworks[$k]['href']=site_url('space/work/'.$work['mid'].'/'.$work['id']);
				}
			}
			$mlist[$key]['works']=$sworks;
			if(!empty($mlist[$key]['works'])){
				$mlist[$key]['uptime']=date('Y.m.d',$mlist[$key]['works'][0]['puttime']);
				$mlist[$key]['title']=$mlist[$key]['works'][0]['title'];
				$mlist[$key]['content']=$mlist[$key]['works'][0]['content'];
				$mlist[$key]['summary']=$mlist[$key]['works'][0]['summary'];
				$mlist[$key]['id']=$mlist[$key]['works'][0]['id'];
				$mlist[$key]['image']=$mlist[$key]['works'][0]['image'];
			}else{
				$mlist[$key]['uptime']='';
				$mlist[$key]['title']='';
				$mlist[$key]['content']='';
				$mlist[$key]['summary']='';
				$mlist[$key]['id']='0';
				$mlist[$key]['image']='';
			}
			$mlist[$key]['content']=empty($mlist[$key]['content'])?'未填写':csubstr($mlist[$key]['content'],45);
			if($value['mid']==$this->member->mid){
				$mlist[$key]['spaceurl']=site_url('member');
				if(empty($mlist[$key]['id'])){
					$mlist[$key]['href']=$mlist[$key]['spaceurl'];
				}else{
					$mlist[$key]['href']=site_url('member/work/'.$mlist[$key]['id']);
				}
				
			}else{
				$mlist[$key]['spaceurl']=site_url('space/'.$value['mid']);
				if(empty($mlist[$key]['id'])){
					$mlist[$key]['href']=$mlist[$key]['spaceurl'];
				}else{
					$mlist[$key]['href']=site_url('space/work/'.$value['mid'].'/'.$mlist[$key]['id']);
				}
			}
			
			if(!empty($ud['goods'][$value['id']])){
				$mlist[$key]['gooded']=$ud['goods'][$value['id']];
			}
			if(!empty($ud['watchs'][$value['id']])){
				$mlist[$key]['watched']=$ud['watchs'][$value['id']];
			}
		}

		$res = array(
			'currentcolumn'=>'leader',
			'currentnav'=>'骚客领袖',
			'leaderlist'=>$mlist,
			'isajax'=>$this->input->is_ajax_request(),
			'latest'=>$this->getLatest()
		);
		if($res['isajax']){
			$this->view('leader-ajax',$res);
		}else{
			$this->view('leader',$res);
		}
	}
}